package com.zhonglz.system.controller;


import java.security.PublicKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonglz.commons.base.BaseController;
import com.zhonglz.model.BuCertInfo;
import com.zhonglz.model.SysFileInfo;
import com.zhonglz.system.domian.SetCertMain;
import com.zhonglz.util.result.ResultUtils;
import com.zhonglz.util.tools.DataGrid;
import com.zhonglz.util.tools.ObjectUtils;

/****
 * 证书接口
 * @author zhonglz
 */
@RestController
@RequestMapping("/sys/cert")
public class CertController extends BaseController{
	
	/****
	 * 分页查询证书信息
	 * @param startTime
	 * @param endTime
	 * @param pageNumber
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ResponseBody
    @RequestMapping("selectListByData")
    public DataGrid selectListByData(BuCertInfo buCertInfo,
    		@RequestParam(defaultValue="") String startTime,
    		@RequestParam(defaultValue="") String endTime,
    		@RequestParam(defaultValue = "1") int pageNumber,
			@RequestParam(defaultValue = "10") int pageSize,
			HttpServletRequest req) {
    	DataGrid dataGrid = null;
		// 封装查询条件参数
		Map<String, Object> mapParam = new HashMap<String, Object>();
		if(ObjectUtils.isNotEmpty(startTime)){
			startTime+=" 00:00:00";
			mapParam.put("startTime",startTime);
		}
		if(ObjectUtils.isNotEmpty(endTime)){
			endTime+=" 23:59:59";
			mapParam.put("endTime",endTime);
		}
		if(ObjectUtils.isNotEmpty(buCertInfo)){
			if(ObjectUtils.isNotEmpty(buCertInfo.getCertCn())) {
				mapParam.put("certCn",buCertInfo.getCertCn());
			}
		}
		
		PageHelper.startPage(pageNumber, pageSize);
		List<BuCertInfo> selectFileInfoList=buCertInfoService.selectByDataList(mapParam);
		PageInfo<BuCertInfo> info = new PageInfo<BuCertInfo>(selectFileInfoList);
		// 封装结果集
		dataGrid = new DataGrid(info.getTotal(),selectFileInfoList,info.getPages(),info.getPageNum(),ResultUtils.CODE_SUCCESS);
    	return dataGrid; 
	}
	
	@ResponseBody
	@RequestMapping("selectList")
	public List<BuCertInfo> selectList() {
		Map<String, Object> mapParam = new HashMap<String, Object>();
		List<BuCertInfo> selectFileInfoList=buCertInfoService.selectByDataList(mapParam);
		return selectFileInfoList;
	}
	
	@ResponseBody
	@RequestMapping("selectInfoById")
	public BuCertInfo selectInfoById(String id) {
		 return buCertInfoService.selectById(id);
	}
	
	@ResponseBody
	@RequestMapping("checkCertPwd")
	public ResultUtils checkCertPwd(String id,String pwd){
		BuCertInfo info = buCertInfoService.selectById(id);
		if(info.getCertPwd().equals(pwd)) {
			info.setCertPwd("");
			return new ResultUtils(ResultUtils.CODE_SUCCESS,"认证成功！",info);
		}
		return new ResultUtils(ResultUtils.CODE_ERROR,"认证失败！");
	}
	
	/****
	 * 软证书生成
	 * @param setCertMain
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/setCert", method =RequestMethod.POST )
	public ResultUtils setCert(SetCertMain setCertMain){
		ResultUtils result =buCertInfoService.setCert(setCertMain);
		return result;
	}
	
	/***
	 * 证书签名
	 * @param algorithmType  证书算法 sm2 rsa
	 * @param certFile 证书文件
	 * @param originalText 签名文本
	 * @param passWord 证书密码
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("signSm2Cert")
	public ResultUtils signSm2Cert(String algorithmType ,MultipartFile certFile, String originalText, String passWord){
		ResultUtils result =buCertInfoService.signSm2Cert(certFile, originalText, passWord);
		return result;
	}

	/****
	 * sm2验签
	 * @param publicKey 公钥
	 * @param originalText 签名原文
	 * @param signData 签名值
	 * @return
	 */
	@ResponseBody
	@RequestMapping("verifySM2Cert")
	public ResultUtils verifySM2Cert(String publicKey, String originalText, String signData){
		ResultUtils result =buCertInfoService.verifySM2Cert(publicKey, originalText, signData);
		return result;
	}
	

	/****
	 * rsa验签
	 * @param certFile 证书文件
	 * @param passWord 证书密码
	 * @param originalText 签名原文
	 * @param signData 签名值
	 * @return
	 */
	@ResponseBody
	@RequestMapping("verifyRSACert")
	public ResultUtils verifyRSACert(MultipartFile certFile,String passWord, String originalText, String signData){
		ResultUtils result =buCertInfoService.verifyRSACert(certFile, passWord,  originalText, signData);
		return result;
	}
	
	
	
	

}
